package com.ruoyi.hikvision.config;

import com.ruoyi.hikvision.hk.HikvisionClient;
import com.ruoyi.hikvision.hk.HikvisionService;
import com.ruoyi.wisdom.domain.IotPickDevice;
import com.ruoyi.wisdom.service.IIotPickDeviceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class HikvisionConfig {

    @Value("${hikvision.user}")
    private String user;

    @Value("${hikvision.password}")
    private String password;

    @Value("${hikvision.port}")
    private short port;

    @Resource
    private IIotPickDeviceService iotPickDeviceService;

    @Bean
    public void initHikvisionDevice() {
        HikvisionService hikvisionService = HikvisionService.getInstance();
        hikvisionService.initSDK();
        IotPickDevice deviceParams = new IotPickDevice();
        List<IotPickDevice> deviceList = iotPickDeviceService.selectIotPickDeviceList(deviceParams);
        for (IotPickDevice pickDevice : deviceList) {
            HikvisionClient device = hikvisionService.getDevice(pickDevice.getIp(), port, user, password);
            if (device != null) {
                device.setDeviceNo(pickDevice.getDeviceNo());
                device.setupAlarmChan();
                System.out.println("[" + pickDevice.getIp() + "]设备登录成功");
            } else {
                System.out.println("[" + pickDevice.getIp() + "]设备登录失败");
            }
        }
    }

}